<template>
  <div class="cesium-draw">
    <div id="cesium-drawer"></div>
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <div class="slider-box-wrapper"><slider-box v-model:value="step" :min="1" :max="100" :step="1" /></div>
      <div class="slider-box-wrapper"><slider-bar v-model:value="curTime" :min="0" :max="timeDuration" :step="1" /></div>
      <a class="tools" @click="togglePlay" :title="playing ? '暂停' : '播放'">
        <PlayCircleOutlined v-if="!playing" />
        <PauseCircleOutlined v-if="playing" />
      </a>

      <!--      <div class="select-box-wrapper"><select-box :options="commonMaterials" /></div>
      <div class="select-box-wrapper"><select-box :options="baseMaterials" /></div>
      <div class="select-box-wrapper"><select-box :options="proceduralTextures" /></div>
      <div class="select-box-wrapper"><select-box :options="miscMaterials" /></div>
      <div class="select-box-wrapper"><select-box :options="compositeMaterials" /></div>
      <div class="select-box-wrapper"><select-box :options="polylineMaterials" /></div> -->

      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="shadows">shadows</a-checkbox></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="softShadows">soft shadows</a-checkbox></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="terrainShadows">terrain shadows</a-checkbox></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="picking">选择</a-checkbox></div> -->
      <div id="menu">
        <!-- <p><button @click="onClick('play')">播放</button></p> -->
        <!-- <p><button @click="onClick('stop')">暂停</button></p> -->
      </div>
    </div>
  </div>
</template>

<script>
// http://earthsdk.com/v/last/Apps/Examples/?menu=true&url=./earth-heatmap-kriging.html
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { PlayCircleOutlined, PauseCircleOutlined, PlayCircleFilled, PauseCircleFilled } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';
import selectBox from '@components/widgets/select-box';
import sliderBox from '@components/widgets/slider-box';
import sliderBar from '@components/widgets/slider-bar';
import HeatMap from '../utils/heatMap/HeatMap';

export default defineComponent({
  name: 'GroundImage', // 贴地图像
  components: { PlayCircleOutlined, PauseCircleOutlined, DownOutlined, EditOutlined, sliderBox, sliderBar, selectBox },
  setup() {
    return {
      viewer: null,
      timer: 0,
      synchronizer: null,
      isRepeating: true,
      videoOverlay: true
    };
  },
  data() {
    return {
      timeDuration: 1000,
      step: 1,
      curTime: 1,
      nowPostion: {},
      playing: true
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    togglePlay() {
      let vm = this;
      this.playing = !this.playing;
    },
    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },
    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.default;
      let osm = new Cesium.ArcGisMapServerImageryProvider({
        // url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
        url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
      });
      var viewer = new Cesium.Viewer('cesium-drawer', {
        // imageryProvider: osm,
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        // shadows: true,
        animation: false,
        // baseLayerPicker: false,
        fullscreenButton: false,
        vrButton: false,
        homeButton: false,
        timeline: false,
        selectionIndicator: false,
        infoBox: false,
        // 注释时相当于使用默认地形，解开注释相当于使用全球地形
        terrainProvider: Cesium.createWorldTerrain(),
        shouldAnimate: true
      });

      //开启深度检测
      // viewer.scene.globe.depthTestAgainstTerrain = true;

      // viewer.imageryLayers._layers.forEach(e=>e.show=false);

      this.viewer = viewer;
      this.onViewerInited(viewer);
      Cesium.GroundPrimitive.initializeTerrainHeights().then(() => {
        vm.initEffects();
      });

      // this.viewer.flyTo(this.viewer.entities);
    },
    initEffects() {
      let vm = this;
      const viewer = this.viewer;
      const config = {
        position: [2.0313527955350708, 0.6964650964888595, 4000],
        origin: [0.1, 0.9],
        // 在地球上的实际尺寸，单位是米
        width: 10000,
        height: 10000,
        // 调色板参数，第一个代码值域，范围在0-1之间，第二个为css颜色字符串
        gradient: [[0, 'rgba(0, 255, 0, 0.1)'], [0.2, 'rgba(0, 0, 255, 0.1)'], [0.6, 'rgba(255, 255, 0, 0.5)'], [1, 'red']],
        maxValue: 1000
      };

      var h = new HeatMap(viewer, config);

      // 演示1：设置网格数据
      /*
      {
           var gridData = new Array(100*100);
           gridData.fill(0);
           // 采样点数组
           const posValues = [[10, 10, 100], [10, 90, 500], [90, 10, 300], [90, 90, 1000]];
           // 通过克里金插值获得网格数组
           HeatMap.getGridDataFromKriging(posValues, 100, 100, gridData);
           h.setGridData(gridData);
      }
      */
      // 演示2：通过插值的方式设置网格数据
      {
        // 采样点数组0
        var gridData0 = new Array(100 * 100);
        gridData0.fill(0);
        const posValues0 = [[10, 10, 0], [10, 90, 0], [50, 50, 0], [90, 10, 0], [90, 90, 0]];
        // 通过克里金插值获得网格数组1
        HeatMap.getGridDataFromKriging(posValues0, 100, 100, gridData0);

        var gridData1 = new Array(100 * 100);
        gridData1.fill(0);
        const posValues1 = [[10, 10, 100], [10, 90, 500], [50, 50, 800], [90, 10, 300], [90, 90, 1000]];
        // 通过克里金插值获得网格数组1
        HeatMap.getGridDataFromKriging(posValues1, 100, 100, gridData1);

        var gridData2 = new Array(100 * 100);
        gridData2.fill(0);
        const posValues2 = [[10, 10, 500], [10, 90, 1000], [50, 50, 200], [90, 10, 1000], [90, 90, 100]];
        // 通过克里金插值获得网格数组1
        HeatMap.getGridDataFromKriging(posValues2, 100, 100, gridData2);

        var gridData3 = new Array(100 * 100);
        gridData3.fill(0);
        const posValues3 = [[10, 10, 1000], [10, 90, 100], [50, 50, 0], [90, 10, 1000], [90, 90, 500]];
        // 通过克里金插值获得网格数组1
        HeatMap.getGridDataFromKriging(posValues3, 100, 100, gridData3);

        var gridData4 = new Array(100 * 100);
        gridData4.fill(0);
        const posValues4 = [[10, 10, 0], [10, 90, 0], [50, 50, 0], [90, 10, 0], [90, 90, 0]];
        // 通过克里金插值获得网格数组1
        HeatMap.getGridDataFromKriging(posValues4, 100, 100, gridData4);

        let ratio = 0;
        var gridData = new Array(100 * 100);
        console.log(ratio);
        viewer.scene.preUpdate.addEventListener(() => {
          if (vm.playing) {
            ratio += 0.001;
          } else {
            ratio = vm.curTime / vm.timeDuration;
          }
          if (ratio > 1.0) {
            ratio = 0.0;
          }
          vm.curTime = vm.timeDuration * ratio;

          gridData.fill(0);
          HeatMap.interpolateGridData(ratio, [gridData0, gridData1, gridData2, gridData3, gridData4], 100 * 100, gridData);
          h.setGridData(gridData);
        });
      }
      h.flyTo();
    }
  }
});
</script>

<style scoped lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    :deep(.cesium-viewer-bottom) {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  .tools {
    // position: absolute;
    top: 57px;
    left: 7px;
    font-size: 1.5em;
    padding: 4px 14px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 5px;
    opacity: 0.6;
    transition: all 500ms;
    &:hover {
      opacity: 0.8;
    }
  }

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 18em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }

  .select-box-wrapper {
    padding: 5px 15px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
    width: auto;
  }
}
</style>
